<?php
/*Problem Domain
To authenticate the validated user and show the appropriate view
*/
/*
include_once 'dao/UserDAOImpl.php';
include_once 'model/ListUserType.php';
include_once 'model/User.php';
*/

foreach (glob("model/*.php") as $filename)
{
      include_once $filename;
} 

foreach (glob("dao/*.php") as $filename)
{
       include_once $filename;
}
	
$email_id = $_POST['txtemail'];
$password = $_POST['txtpw'];

/*void -> boolean
purpose. authenticates the User and redirects the user to appropriate view
i.e if user is authenticated as admin then it redirects to admin home page
else if trainee it redirects to trainee home page.
else returns false*/
function authnRedirect()
{
	global $email_id;
	global $password;
	$auth_ok = 0;
	$usr = new UserDAOImpl();
	
	$array_list = $usr->listUsers()->getArrayList();

	foreach($array_list as $user)
	{
		if(($user->getEmailId() == $email_id) && ($user->getPassword() == $password))
		{
			$auth_ok = 1;
			break;	
		}
	}
	if(!$auth_ok)
        {
                header('Location: login.html');
        }
        else
        {
		if($user->getRole() == 'admin')
		{
                	header('Location: views/home_admin.php');
		}
		else
		{
			header("Location: views/home_user.php?user_id=".$user->getId());
		}
        }
}

authnRedirect();
?>
